package com.sea.product.controller;

import com.codecrane.core.power.service.DictDetailService;
import com.codecrane.core.power.service.PowerGroupService;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.product.entity.NewProduct;
import com.sea.product.entity.NewProductRecord;
import com.sea.product.service.CategoryService;
import com.sea.product.service.ChannelService;
import com.sea.product.service.NewProductRecordService;
import com.sea.product.service.NewProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping(value = "/sea/product/record")
public class NewProductRecordController {

    @Autowired
    private NewProductService newProductService;
    @Autowired
    private NewProductRecordService newProductRecordService;
    /**
     * 管理主页面
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model, @RequestParam("id")Integer id) {
        model.addAttribute("productId",id);
        return "/sea/product/newProduct/record";
    }
    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByProductId")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     Integer productId
    ) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<NewProductRecord> list = newProductRecordService.findByProductId(productId);
        PageInfo<NewProductRecord> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    /**
     * 保存
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxReturn save(@RequestParam("productId")Integer productId,
                           @RequestParam("productStatus")Integer productStatus,
                           @RequestParam("productRemark")String productRemark,
                           @RequestParam("productTime")String productTime) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try{
            if (newProductRecordService.save(productId,productStatus,productRemark,productTime) > 0) {
                result.setOk(true).setMsg("保存成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

    /**
     * 保存
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/usave", method = RequestMethod.POST)
    public AjaxReturn usave(NewProductRecord newProductRecord) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try{
            if ( null != newProductRecord && newProductRecordService.usave(newProductRecord,newProductRecord.getFlag()) > 0) {

                result.setOk(true).setMsg("保存成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }
}
